package downloader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

public class IODownloader implements Downloader {
    public IODownloader() {
        System.out.println("使用IO模式下载网页内容");
    }

    @Override
    public String downloader(Properties properties) {
        String html = "";
        // 通过 ['try' with resources] 的方式自动关闭资源
        try (
                BufferedReader in = new BufferedReader(new InputStreamReader(new URL(properties.getProperty("url")).openConnection().getInputStream()));
        ) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                html += inputLine;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return html;
    }
}
